package controller;

import DynamicProxy.entity.Person;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LangController {

   private  int a = 12;
   private static ThreadLocal<Integer> local = new ThreadLocal<Integer>();
   private static InheritableThreadLocal<Integer> localInheritable = new InheritableThreadLocal<Integer>();

    private static ExecutorService executor = Executors.newFixedThreadPool(5);
    /**
     * clone复制对象
     * 克隆是新的对象，赋值只是引用的赋给它
     */
    public static void cloneLong() throws CloneNotSupportedException {
        Person person = new Person("mwh",20);
        Person person1 =  (Person) person.clone();
        Person person2 = person;

        System.out.println("克隆的相同："+(person1 == person));
        System.out.println("赋值的相同："+(person2 == person));

        person.setName("hht");

        System.out.println("变化后克隆的:"+person1.getName());//mwh
        System.out.println("变化后赋值的:"+person2.getName());//hht
    }


    public   void threadLocalTest(){
        Thread thread = new Thread(){
            @Override
            public void run() {
                local.set(4);
                System.out.println(Thread.currentThread().getName()+"==="+local.get());
            }
        };

        Thread thread1 = new Thread(){
            @Override
            public void run() {
                local.set(2);
                System.out.println(Thread.currentThread().getName()+"==="+local.get());
            }
        };

        thread.start();
        thread1.start();

    }
    public   void inheritablThreadLocalTest(){
        localInheritable.set(3);
        Thread thread = new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"==="+localInheritable.get());//----3
                localInheritable.set(4);
                System.out.println(Thread.currentThread().getName()+"==="+localInheritable.get());//-----4
            }
        };

        Thread thread1 = new Thread(){
            @Override
            public void run() {
                localInheritable.set(2);
                System.out.println(Thread.currentThread().getName()+"==="+localInheritable.get());//-----2
            }
        };

        thread.start();
        thread1.start();

    }

    /**
     * 变量没放threadlocal,共享变量
     */
    public   void nothreadLocalTest(){


        Thread thread = new Thread(){
            @Override
            public void run() {
                a++;
                System.out.println(Thread.currentThread().getName()+"==="+a);
            }
        };

        Thread thread1 = new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"==="+a);
            }
        };

        thread.start();
        thread1.start();

    }


    /**
     * 子线程类
     */
    class MyThread extends Thread {

        private  boolean flag = false;

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                System.out.println("重新启动");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 修改变量值
            flag = true;
            System.out.println("flag = " + flag);
        }

        public boolean isFlag() {
            return flag;
        }

        public void setFlag(boolean flag) {
            this.flag = flag;
        }
    }


    public void testVoliate(){
        MyThread myThread = new MyThread();
        // 开启线程
        myThread.start();
       boolean flag = myThread.isFlag();
        System.out.println("falg:"+flag);

        int i =0;
        // 主线程执行
        while(i<3){
            if (flag) {
                i++;
                System.out.println("主线程访问到 flag 变量");
            }
        }

    }

}
